package cn.edu.thu.tsquality.core.algorithm.sequential.dependencies;

import cn.edu.thu.tsquality.adaptor.single.SingleAdaptor;
import cn.edu.thu.tsquality.core.common.algorithm.IAlgorithm;
import cn.edu.thu.tsquality.core.common.util.ArgumentsHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



public class SingleMain
{
    public static void main(String[] args) throws IOException
    {

        System.out.println("SequentialDependencies");
        System.out.println("args length="+args.length);
        for(int i = 0; i < args.length; i++)
            System.out.println("arg[" + i + "] = " + args[i]);

        String inputPath = args[0];
        String outputPath = args[1];


        List<String> usedAttrs;
        String attr;
        Double leftBound, rightBound;
        Double confidence;
        Integer support;

        // 这个参数用于确定放大区间的情况，可变，但没有在前端展示出来。
        // 将区间放大为 (1+eps) 倍，放大后区间置信度 >= 1/(1+eps) * 原置信度
        // 生成的区间长度为：(1+delta)^h, 满足 0 < delta <= eps/(2+eps)
        Double eps = 0.1;

        try
        {
            String[] arguments = ArgumentsHelper.getArguments(args[2]);

            if(arguments.length != 3)
                throw new IllegalArgumentException("argument error, expected 3 arguments, got "+args.length);

            // arg[0] {attr} 第一个参数，选择列名进行检测
            String[] attrArgs = ArgumentsHelper.getValues(arguments[0]);
            if(attrArgs.length == 1)
                attr = attrArgs[0];
            else
                throw new IllegalArgumentException("attr error, expected 1 args, got "+attrArgs.length);
            usedAttrs = new ArrayList<>(Arrays.asList(attrArgs));

            // arg[1] {leftBound, rightBound} 第二个参数，相邻数据变化区间
            String[] intervalBoundArgs = ArgumentsHelper.getValues(arguments[1]);
            if(intervalBoundArgs.length == 2)
            {
                leftBound = Double.parseDouble(intervalBoundArgs[0]);
                rightBound = Double.parseDouble(intervalBoundArgs[1]);
            }
            else
            {
                throw new IllegalArgumentException("intervalBound error, expected 2 args, got " + intervalBoundArgs.length);
            }

            // arg[2] {confidence,support} 第三个参数，置信度和支持度
            String[] otherArgs = ArgumentsHelper.getValues(arguments[2]);
            if (otherArgs.length!=2)
                throw new IllegalArgumentException("confidence/support error, expected 2 args, got "+otherArgs.length);
            confidence = Double.parseDouble(otherArgs[0]);
            support = Integer.parseInt(otherArgs[1]);

            IAlgorithm algorithm = new SequentialDependencies(attr, leftBound, rightBound, confidence, support, eps);
            SingleAdaptor adaptor = new SingleAdaptor();
            adaptor.setAlgorithm(algorithm);

            // long startTime = System.currentTimeMillis();

            adaptor.run(inputPath, outputPath, usedAttrs);

//            long endTime = System.currentTimeMillis();
//            System.out.println("程序运行时间：" + 1.0*(endTime - startTime) + "ms");

        }
        catch (Exception e)
        {
            System.out.println("Catch Exception: "+e.toString());
            throw e;
        }





    }
}
